<?php

// start session
session_start();

// connect to database
$conn_str = 'host=dbsrv1.cdf.toronto.edu dbname=csc309g23 user=csc309g23 password=ipheid5h';
$conn = pg_connect($conn_str);

// determine the function
if ($_POST['func'] == 'addSport') {
    $query = 'INSERT INTO sports VALUES (\'' . $_POST['sport'] . '\')';
} else if ($_POST['func'] == 'removeSport') {
    $query = 'DELETE FROM sports WHERE sport=\'' . $_POST['sport'] . '\'';
} else if ($_POST['func'] == 'addCountry') {
    $query = 'INSERT INTO countries VALUES (\'' . $_POST['country'] . '\')';
} else if ($_POST['func'] == 'removeCountry') {
    $query = 'DELETE FROM countries WHERE sport=\'' . $_POST['country'] . '\'';
}

// query database
$result = pg_query($conn, $query);

// go back to page from which user logged in
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
